Code
pacman::p_load(ggiraph, plotly, gganimate, DT, tidyverse, patchwork, gifski, gapminder, readxl, rPackedBar)January 23, 2024
January 24, 2024
The code chunk below uses p_load() of pacman package to check if packages are installed in the computer. If they are, then they will be launched into R. The R packages installed are:
ggiraph for making ‘ggplot’ graphics interactive.
plotly, R library for plotting interactive statistical graphs.
gganimate, an ggplot extension for creating animated statistical graphs.
DT provides an R interface to the JavaScript library DataTables that create interactive table on html page.
tidyverse, a family of modern R packages specially designed to support data science, analysis and communication task including creating static statistical graphs.
patchwork for compising multiple plots.
gifski converts video frames to GIF animations using pngquant’s fancy features for efficient cross-frame palettes and temporal dithering. It produces animated GIFs that use thousands of colors per frame.
gapminder: An excerpt of the data available at Gapminder.org. We just want to use its country_colors scheme.
Importing the data
ggiraph for interactive data visualizationggiraph package
Interactive Arguments of ggiraph
Tooltip: displayed when hovering over chart elements
Onclick: executed when elements are clicked
Data_id: Uses columns as id to show asan sociation of elements
If it is used within a shiny application, elements associated with an id (data_id) can be selected and manipulated on client and server sides.
tooltip (tooltip effect)There are two parts of the codes: 1. creating ggplot object, 2. girafe() of ggiraph will be used to create an interactive svg object.
Interactivity: By hovering the mouse pointer on an data point of interest, the student’s ID will be displayed.
Interactivity: By hovering the mouse pointer on an data point of interest, the student’s ID and Class will be displayed.
#Creating new field called tooltip
exam_data$tooltip <- c(paste0(
"Name = ", exam_data$ID,
"\n Class", exam_data$CLASS
))
my_plot2 <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = exam_data$tooltip), #refer to the tooltip field above
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = my_plot2,
width_svg = 8,
height_svg = 8*0.618
)Using opts_tooltip to customise tooltip rendering by adding css declaration
tooltip_css <- "background-color:white; font-style:bold; color:red;"
my_plot3 <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = my_plot3,
width_svg = 6,
height_svg = 6*0.618,
options = list(
opts_tooltip(
css = tooltip_css)
)
)Note: Background color is now white and the font color is red and bold
In this example, a function is used to compute 90% confident interval of the mean. The derived statistics are then displayed in the tooltip. This is created by creating a function
tooltip_fn <- function(y, ymax, accuracy = .01) {
mean <- scales::number(y, accuracy = accuracy)
sem <- scales::number(ymax - y, accuracy = accuracy)
paste("Mean maths scores:", mean, "+/-", sem)
}
gg_point <- ggplot(data = exam_data,
aes(x = RACE)) +
stat_summary(
aes(y = MATHS,
tooltip = after_stat(
tooltip_fn(y, ymax))),
fun.data = "mean_se",
geom = GeomInteractiveCol,
fill = "lightblue"
) +
stat_summary(
aes(y = MATHS),
fun.data = "mean_se",
geom = "errorbar", width = 0.2, linewidth = 0.2
)
girafe(
ggobj = gg_point,
width_svg = 8,
height_svg = 8*0.618,
)data_id (hover effect)Interactivity: Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over. Note that the default value of the hover css is hover_css = “fill:orange;”.
Using css declaration to change the highlighting effect
my_plot2 <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(data_id = CLASS),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = my_plot2,
width_svg = 6,
height_svg = 6*0.618,
options = list(
opts_hover(css = "fill: pink;"),
opts_hover_inv(css = "opacity:0.2;")
)
)Interactivity: Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over. Notice opts_hover refers to the selected data and opts_hover_inv refers to the non-selected data. Different from section 2.1.3 above, the css customisation request are encoded directly.
my_plot_comb <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = CLASS,
data_id = CLASS),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = my_plot_comb,
width_svg = 6,
height_svg = 6*0.618,
options = list(
opts_tooltip(css = "background-color:white; font-style:bold; color:green;"),
opts_hover(css = "fill: pink;"),
opts_hover_inv(css = "opacity:0.2;")
)
)Interactivity: Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over. At the same time, the tooltip will show the CLASS.
onclick (click effect)onclick argument of ggiraph provides hotlink interactivity on the web.
exam_data$onclick <- sprintf("window.open(\"%s%s\")",
"https://www.moe.gov.sg/schoolfinder?journey=Primary%20school",
as.character(exam_data$ID))
my_plot <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(onclick = onclick),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = my_plot,
width_svg = 6,
height_svg = 6*0.618
)Interactivity: Web document link with a data object will be displayed on the web browser upon mouse click. Note that click actions must be a string column in the dataset containing valid javascript instructions.
ggiraphCoordinated multiple views methods is interactive in which when a data point of one of the dotplot is selected, the corresponding data point ID on the second data visualisation will be highlighted too.
In order to build a coordinated multiple views, the following programming strategy will be used:
Appropriate interactive functions of ggiraph will be used to create the multiple views.
patchwork function of patchwork package will be used inside girafe function to create the interactive coordinated multiple views.
p1 <- ggplot(data = exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = ID,
data_id = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
coord_cartesian(xlim = c(0,100)) +
scale_y_continuous(NULL,
breaks = NULL)
p2 <- ggplot(data = exam_data,
aes(x = ENGLISH)) +
geom_dotplot_interactive(
aes(tooltip = ID,
data_id = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
coord_cartesian(xlim = c(0,100)) +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
code = print(p1 / p2),
width_svg = 6,
height_svg = 6,
options = list(
opts_hover(css = "fill: blue;"),
opts_hover_inv(css = "opacity:0.2;")
)
)The data_id aesthetic is critical to link observations between plots and the tooltip aesthetic is optional but nice to have when mouse over a point.
plotly method for interactive data visualizationPlotly’s R graphing library create interactive web graphics from ggplot2 graphs and/or a custom interface to the (MIT-licensed) JavaScript library plotly.js inspired by the grammar of graphics.
Different from other plotly platform, plot.R is free and open source.
There are two ways to create interactive graph by using plotly, they are:
by using plot_ly(), and
by using ggplotly()
plot_lyCreating basic interactive scatterplot
Changing the default color pallete to ColorBrewel colour palette
Customising the color scheme manually
Customising tooltip
Working with layout. To learn more about layout, visit this link.
ggplotlyCreating basic interactive scatterplot. With ggplotly, we can use the original ggplot2 and add ggplotly at the end as extra line
Creating Multiple Views using highlight_key and subplot of plotly package
d <- highlight_key(exam_data)
p1 <- ggplot(data=d,
aes(x = MATHS,
y = ENGLISH)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
p2 <- ggplot(data=d,
aes(x = MATHS,
y = SCIENCE)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
subplot(ggplotly(p1),
ggplotly(p2))Click on a data point of one of the scatterplot and see how the corresponding point on the other scatterplot is selected.
crosstalk method for interactive data visualizationCrosstalk is an add-on to the htmlwidgets package. It extends htmlwidgets with a set of classes, functions, and conventions for implementing cross-widget interactions (currently, linked brushing and filtering).
A wrapper of the JavaScript Library DataTables
Data objects in R can be rendered as HTML tables using the JavaScript library ‘DataTables’ (typically via R Markdown or Shiny).
crosstalk methodThings to learn from the code chunk:
highlight() is a function of plotly package. It sets a variety of options for brushing (i.e., highlighting) multiple plots. These options are primarily designed for linking multiple plotly graphs, and may not behave as expected when linking plotly to another htmlwidget package via crosstalk. In some cases, other htmlwidgets will respect these options, such as persistent selection in leaflet.
bscols() is a helper function of crosstalk package. It makes it easy to put HTML elements side by side. It can be called directly from the console but is especially designed to work in an R Markdown document. Warning: This will bring in all of Bootstrap!.
gganimate method for creating animationgganimate extends the grammar of graphics as implemented by ggplot2 to include the description of animation. It does this by providing a range of new grammar classes that can be added to the plot object in order to customise how it should change with time.
transition_*() defines how the data should be spread out and how it relates to itself across time.
view_*() defines how the positional scales should change along the animation.
shadow_*() defines how data from other points in time should be presented in the given point in time.
enter_*()/exit_*() defines how new data should appear and how old data should disappear during the course of the animation.
ease_aes() defines how different aesthetics should be eased during transitions.
Import data from the Data worksheet from GlobalPopulation Excel workbook.
Basic ggplot function to create static bubble plot

Building animated bubble plot
ggplot(globalPop, aes(x = Old, y = Young,
size = Population,
colour = Country)) +
geom_point(alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young') +
transition_time(Year) +
ease_aes('linear') 
ggplotly() methodcreate an animated bubble plot by using ggplotly() method.
Things to learn from the code chunk above
Appropriate ggplot2 functions are used to create a static bubble plot. The output is then saved as an R object called gg.
ggplotly() is then used to convert the R graphic object into an animated svg object.
Notice that although show.legend = FALSE argument was used, the legend still appears on the plot. To overcome this problem, theme(legend.position='none') should be used as shown in the plot and code chunk below.
gg <- ggplot(globalPop,
aes(x = Old,
y = Young,
size = Population,
colour = Country)) +
geom_point(aes(size = Population,
frame = Year),
alpha = 0.7) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(x = '% Aged',
y = '% Young') +
theme(legend.position='none')
ggplotly(gg)plot_ly() methodIn this sub-section, you will learn how to create an animated bubble plot by using plot_ly() method.